"""
单词计数
"""
wc = dict() # 存储单词出现的次数 以单词为key  以次数为value
with open("word.txt","r",encoding="utf-8") as f,open("wc.txt","w",encoding="utf-8") as g:
    # 读取文件中所有数据
    while len(line := f.readline()) != 0:
        words = line.split(" ")
        for word in words:
            word = word.replace("\n","")
            old_count = wc.get(word,0)
            wc[word] = old_count + 1

    # 将字典中的数据以 单词  次数 写到wc.txt
    for word,count in wc.items():
        g.write(f"{word} {count}\n")
        g.flush()